#include"InetAddress.h"

// #include "InetAddress.h"

// #include <strings.h>
// #include <string.h>

    InetAddress::InetAddress(uint16_t port, std::string Ip)
    {
        bzero(&addr_, sizeof addr_);
        addr_.sin_port = htons(port);
        addr_.sin_family = AF_INET;
        addr_.sin_addr.s_addr = inet_addr(Ip.c_str());
    }
    InetAddress::InetAddress(const sockaddr_in addr) : addr_(addr) {}
    int16_t InetAddress::toPort() const
    {
        return ntohs(addr_.sin_port);
    }
    std::string InetAddress::toIp() const
    {
        char buf[64] = {0};
        ::inet_ntop(AF_INET, &addr_.sin_addr, buf, sizeof buf);
        return buf;
    }
    std::string InetAddress::toIpPort() const
    {
        char buf[64]={0};
        ::inet_ntop(AF_INET,&addr_.sin_addr,buf,sizeof buf);
        int16_t port=ntohs(addr_.sin_port);
        int end=strlen(buf);
        sprintf(buf+end,":%u",port);
        return buf;
    }
    const sockaddr_in *InetAddress::getSockAddr() const
    {
        return &addr_;
    }


